package guda.house.web.action.sys.owner;

import guda.grape.autogen.common.page.BizResult;
import guda.grape.util.RequestUtil;
import guda.house.biz.OwnerBiz;
import guda.house.biz.beans.vo.JsonResult;
import guda.house.biz.form.OwnerSearchForm;
import guda.house.biz.form.village.OwnerEditForm;
import guda.house.biz.form.village.OwnerForm;
import guda.house.biz.query.OwnerQuery;
import guda.house.biz.util.ErrorCode;
import guda.house.dal.dataobject.*;
import guda.house.web.helper.ResponseHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * Created by well on 15/11/14.
 */
@Controller
public class OwnerAction {

    @Autowired
    private OwnerBiz ownerBiz;

    @RequestMapping(value = "sys/owner/list.htm", method = RequestMethod.GET)
    public String list(HttpServletRequest request, ModelMap modelMap,OwnerSearchForm ownerSearchForm) {
        int pageId = RequestUtil.getInt(request, "pageNo");
        int pageSize = RequestUtil.getInt(request, "pageSize");
        OwnerQuery ownerQuery = new OwnerQuery();
        ownerQuery.setPageNo(pageId);
        ownerQuery.setPageSize(pageSize);
        ownerQuery.setSuiteId(ownerSearchForm.getSuiteId());
        ownerQuery.setVillageId(ownerSearchForm.getVillageId());
        ownerQuery.setBuildingId(ownerSearchForm.getBuildingId());
        ownerQuery.setUnitId(ownerSearchForm.getUnitId());
        modelMap.put("query",ownerQuery);
        BizResult bizResult = ownerBiz.list(ownerQuery);
        BizResult bizResult1 = ownerBiz.initSearchForm(ownerSearchForm);
        modelMap.putAll(bizResult1.data);
        if (bizResult.success) {
            modelMap.putAll(bizResult.data);
            return "sys/owner/list.vm";
        } else {
            return "redirect:/error.htm?code="+bizResult.code;
        }
    }

    @RequestMapping(value = "sys/owner/edit.htm", method = RequestMethod.GET)
    public String edit(HttpServletRequest request, ModelMap modelMap, OwnerEditForm ownerEditForm,
                       BindingResult result, Map<String,Object> model) {
        long id = RequestUtil.getLong(request, "id");
        BizResult bizResult = ownerBiz.detail(id);
        if (bizResult.success) {
            modelMap.putAll(bizResult.data);
            OwnerDO ownerDO = (OwnerDO) bizResult.data.get("ownerDO");
            ownerEditForm.initForm(ownerDO);
            BizResult bizResult1 = ownerBiz.queryForEdit(ownerDO.getSuiteId());
            modelMap.putAll(bizResult1.data);

            SuiteDO suiteDO =  (SuiteDO)bizResult1.data.get("suiteDO");
            UnitDO unitDO = (UnitDO)bizResult1.data.get("unitDO");
            BuildingDO buildingDO = (BuildingDO)bizResult1.data.get("buildingDO");
            ownerEditForm.setVillageId(buildingDO.getVillageId());
            ownerEditForm.setBuildingId(buildingDO.getBuildingId());
            ownerEditForm.setUnitId(unitDO.getUnitId());
            ownerEditForm.setSuiteId(suiteDO.getSuiteId());

            List<ContactDO> contactDOs = (List<ContactDO>)bizResult1.data.get("contactList");
            for(int i=0,len=contactDOs.size();i<len;++i){
                ContactDO contactDO =contactDOs.get(i);
                if(i==0){
                    ownerEditForm.setContactName1(contactDO.getUserName());
                    ownerEditForm.setContactPhone1(contactDO.getPhone());
                    ownerEditForm.setContactRelation1(contactDO.getRelation());
                }
                if(i==1){
                    ownerEditForm.setContactName2(contactDO.getUserName());
                    ownerEditForm.setContactPhone2(contactDO.getPhone());
                    ownerEditForm.setContactRelation2(contactDO.getRelation());
                }
            }

            List<RenderDO> renderDOList = (List<RenderDO>)bizResult1.data.get("renderList");
            for(int i=0,len=renderDOList.size();i<len;++i){
                RenderDO renderDO =renderDOList.get(i);
                if(i==0){
                    ownerEditForm.setRentName1(renderDO.getRenderName());
                    ownerEditForm.setRentPhone1(renderDO.getPhone());

                }
                if(i==1){
                    ownerEditForm.setRentName2(renderDO.getRenderName());
                    ownerEditForm.setRentPhone2(renderDO.getPhone());
                }
            }
            return "sys/owner/edit.vm";
        } else {
            return "redirect:/error.htm?code="+bizResult.code;
        }

    }

    @RequestMapping(value = "sys/owner/detail.htm", method = RequestMethod.GET)
    public String detail(HttpServletRequest request, ModelMap modelMap
                      , Map<String,Object> model) {
        long suiteId = RequestUtil.getLong(request, "suiteId");
        BizResult bizResult = ownerBiz.detailWithSuiteId(suiteId);
        if (bizResult.success) {
            modelMap.putAll(bizResult.data);
            return "sys/owner/detail.vm";
        } else {
            return "redirect:/error.htm?code="+bizResult.code;
        }

    }


    @RequestMapping(value = "sys/owner/create.htm", method = RequestMethod.GET)
    public String create(HttpServletRequest request, ModelMap modelMap, OwnerForm ownerForm,
                         BindingResult result) {
        return "sys/owner/create.vm";
    }

    @RequestMapping(value = "sys/owner/doCreate.htm", method = RequestMethod.POST)
    public String doCreate(HttpServletRequest request, ModelMap modelMap,@Valid OwnerForm ownerForm,
                           BindingResult result) {
        if (result.hasErrors()) {
            BizResult bizResult = ownerBiz.queryForCreateReset(ownerForm.getVillageId(), ownerForm.getBuildingId(), ownerForm.getUnitId());
            modelMap.putAll(bizResult.data);
            return "sys/owner/create.vm";
        }
        BizResult bizResult = ownerBiz.create(ownerForm);
        if (bizResult.success) {
            return "redirect:/sys/owner/list.htm";
        } else {
            return "redirect:/error.htm?code="+bizResult.code;
        }

    }

    @RequestMapping(value = "sys/owner/doUpdate.htm", method = RequestMethod.POST)
    public String doUpdate(HttpServletRequest request, HttpServletResponse response,ModelMap modelMap,@Valid OwnerEditForm ownerEditForm,
                           BindingResult result, Map<String,Object> model) {
        if (result.hasErrors()) {
            BizResult bizResult = ownerBiz.queryForCreateReset(ownerEditForm.getVillageId(), ownerEditForm.getBuildingId(), ownerEditForm.getUnitId());
            modelMap.putAll(bizResult.data);
            return "sys/owner/edit.vm";
        }
        BizResult bizResult = ownerBiz.update(ownerEditForm);
        if (bizResult.success) {
            return "redirect:/sys/owner/list.htm";
        } else {
            return "redirect:/error.htm?code="+bizResult.code;
        }

    }

    @RequestMapping(value = "sys/owner/doDelete.htm")
    public void doDelete(HttpServletRequest request, HttpServletResponse response,ModelMap modelMap) {
        long id = RequestUtil.getLong(request, "id");
        BizResult bizResult = ownerBiz.delete(id);
        JsonResult result = JsonResult.getInstance();
        if (bizResult.success) {
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        } else {
            result.setErrMsg(ErrorCode.getMessage(bizResult.code));
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        }

    }


}
